package com.water.skin.attr

import android.app.Activity
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.water.skin.R
import com.water.skin.constant.SkinConfig
import java.util.*

/**
 *  Updated by zxn on 2021/9/6.
 */
object SkinAttrSupport {

    private fun getSupportAttrType(attrName: String): SkinAttrType? {
        for (attrType in SkinAttrType.values()) {
            if (attrType.attrType == attrName) return attrType
        }
        return null
    }

    /**
     * 传入fragment，找到content元素，递归遍历所有的子View，根据tag命名，记录需要换肤的View
     *
     * @param fragment
     */
    @JvmStatic
    fun getSkinViews(fragment: Fragment): List<SkinView> {
        val skinViews = mutableListOf<SkinView>()
        fragment.view?.let {
            addSkinViews(it, skinViews)
        }
        return skinViews
    }

    @JvmStatic
    fun getSkinViews(view: View): List<SkinView> {
        val skinViews = mutableListOf<SkinView>()
        addSkinViews(view, skinViews)
        return skinViews
    }

    /**
     * 传入activity，找到content元素，递归遍历所有的子View，根据tag命名，记录需要换肤的View
     *
     * @param activity
     */
    @JvmStatic
    fun getSkinViews(activity: Activity): List<SkinView> {
        val skinViews = mutableListOf<SkinView>()
        val content = activity.findViewById<ViewGroup>(android.R.id.content)
        addSkinViews(content, skinViews)
        return skinViews
    }

    @JvmStatic
    fun addSkinViews(view: View, skinViews: MutableList<SkinView>) {
        val skinView = getSkinView(view)
        skinView?.let {
            skinViews.add(it)
        }
        if (view is ViewGroup) {
            var i = 0
            val n = view.childCount
            while (i < n) {
                val child = view.getChildAt(i)
                addSkinViews(child, skinViews)
                i++
            }
        }
    }

    private fun getSkinView(view: View): SkinView? {
        var tag = view.getTag(R.id.skin_tag_id)
        if (tag == null) {
            tag = view.tag
        }
        if (tag == null) return null
        if (tag !is String) return null
        val skinAttrs = parseTag(tag)
        if (skinAttrs.isNotEmpty()) {
            changeViewTag(view)
            return SkinView(view, skinAttrs)
        }
        return null
    }

    private fun changeViewTag(view: View) {
        var tag = view.getTag(R.id.skin_tag_id)
        if (tag == null) {
            tag = view.tag
            view.setTag(R.id.skin_tag_id, tag)
            view.tag = null
        }
    }

    //skin:left_menu_icon:src|skin:color_red:textColor
    private fun parseTag(tagStr: String): List<SkinAttr> {
        val skinAttrs: MutableList<SkinAttr> = ArrayList()
        if (TextUtils.isEmpty(tagStr)) return skinAttrs
        val items = tagStr.split("[|]").toTypedArray()
        for (item in items) {
            if (!item.startsWith(SkinConfig.SKIN_PREFIX)) continue
            val resItems = item.split(":").toTypedArray()
            if (resItems.size != 3) continue
            val resName = resItems[1]
            val resType = resItems[2]
            Log.i("SkinAttrSupport", "parseTag: resName:" + resName + "resType:" + resType)
            val attrType = getSupportAttrType(resType) ?: continue
            val attr = SkinAttr(attrType, resName)
            skinAttrs.add(attr)
        }
        return skinAttrs
    }

    /**
     * @param view 需要改变皮肤的View.
     * @param viewTag 通过tag指定资源名称
     */
    fun changeViewSkinTag(view: View, viewTag: Any) {
        view.setTag(R.id.skin_tag_id, viewTag)
    }
}